/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.RolDao;
import com.tesam.gedeon.entity.Rol;
import com.tesam.gedeon.service.RolService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author joelpacheco
 */
@Service
public class RolServiceImpl implements RolService {

    @Autowired
    private RolDao rolDao;

    @Override
    public int save(Rol rol) {
        return rolDao.save(rol);
    }

    @Override
    public int delete(Rol rol) {
        return rolDao.delete(rol);
    }

    @Override
    public int update(Rol rol) {
        return rolDao.update(rol);
    }

    @Override
    public Rol getById(int id) {
        return rolDao.getById(id);
    }

    @Override
    public List<Rol> getAll() {
        return rolDao.getAll();
    }

    @Override
    public List<Rol> getAllByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
        return rolDao.getAllByLimit(startLimit, endLimit, dataToSort, directionOfOrder);
    }

    @Override
    public List<Rol> searchByName(Rol rol, int satartLimit, int endLimit) {
        return rolDao.searchByName(rol, satartLimit, endLimit);
    }

    @Override
    public int getCountTotal() {
        return rolDao.getCountTotal();
    }

    @Override
    public int getCountSearchByName(Rol rol) {
        return rolDao.getCountSearchByName(rol);
    }

    @Override
    public String getJsonDataFromArrayList(List<Rol> listaDeRoles, int cantidadRegistros, int moduleId) {

        JSONArray array = null;
        JSONObject json = null;

        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listaDeRoles.size() > 0) {


                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");

                for (Rol rol : listaDeRoles) {
                    JSONArray arrayPut = new JSONArray();

                    arrayPut.put(rol.getId());
                    arrayPut.put(rol.getName());

                    if (rol.getDescription() != null) {
                        arrayPut.put(rol.getDescription());
                    } else {
                        arrayPut.put(rol.getDescription());
                    }

                    if (rol.getDateCreated() != null) {
                        arrayPut.put((format.format(rol.getDateCreated())));
                    } else {
                        arrayPut.put(rol.getDateCreated());
                    }
                    // arrayPut.put("<a href=\"#delete\" data-toggle=\"modal\" class=\" btn yellow\" value=\""+rol.getId()+"\">Eliminar</a>");
                    arrayPut.put("<a class=\"btn mini blue\" href=\"/GedeonMvc/rol/view?id=" + rol.getId() + "&module=" + moduleId + "\">"
                            + "<i class=\"icon-share\"></i>View</a>"
                            + " <a class=\"btn mini green\" href=\"/GedeonMvc/rol/edit?id=" + rol.getId() + "&module=" + moduleId + "\">"
                            + "<i class=\"icon-edit\"></i>Editar</a>"
                            + " <a href=\"#delete\" data-toggle=\"modal\" class=\"btn mini red\" value=\"" + rol.getId() + "\"><i class=\"icon-trash\"></i>Eliminar</a>");
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", cantidadRegistros);
            json.put("iTotalDisplayRecords", cantidadRegistros);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
    }

    @Override
    public List<Rol> getAllByProfileId(int profileId) {
        return rolDao.getAllByProfileId(profileId);
    }

    @Override
    public List<Rol> getAllByParentId(int parentId) {
        return rolDao.getAllByParentId(parentId);
    }

    @Override
    public int saveTreeView(int createdby, int ownerid, String treerol) {
        return rolDao.saveTreeView(createdby, ownerid, treerol);
    }

    @Override
    public int updateTreeView(Rol rol) {
        return rolDao.updateTreeView(rol);
    }

    @Override
    public String getTreeView() {
        return rolDao.getTreeView();
    }

    @Override
    public int getCountTreeView() {
        return rolDao.getCountTreeView();
    }

    @Override
    public int getCountByParentId(int id) {
        return rolDao.getCountByParentId(id);
    }

    @Override
    public boolean isNullOrEmpty(String parameter) {
        if (parameter != null) {
            if (!parameter.isEmpty()) {
                return false;
            }
        }
        return true;
    }

    private String convetToUft8(String parameter) {

        int i = 0, len = parameter.length();
        char c;
        StringBuilder sb = new StringBuilder(len);
        while (i < len) {
            c = parameter.charAt(i++);
            if (c == '\\') {
                if (i < len) {
                    c = parameter.charAt(i++);
                    if (c == 'u') {
                        // TODO: check that 4 more chars exist and are all hex digits
                        c = (char) Integer.parseInt(parameter.substring(i, i + 4), 16);
                        i += 4;
                    } // add other cases here as desired...
                }
            } // fall through: \ escapes itself, quotes any character but u
            System.out.println("Letra =>" + StringEscapeUtils.escapeHtml4(String.valueOf(c)));
            sb.append(c);
        }
        return sb.toString();

    }

    private String convertToStringFromUtf8(String parameter) {

        parameter = parameter.replace("\u00e1", "á");
        parameter = parameter.replace("\u00e9", "é");
        parameter = parameter.replace("\u00ed", "í");
        parameter = parameter.replace("\u00f3", "ó");
        parameter = parameter.replace("\u00fa", "ú");
        parameter = parameter.replace("\u00c1", "Á");
        parameter = parameter.replace("\u00c9", "É");
        parameter = parameter.replace("\u00cd", "Í");
        parameter = parameter.replace("\u00d3", "Ó");
        parameter = parameter.replace("\u00da", "Ú");
        return parameter;

    }
}
